#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2025/2/5 16:16
# @Author  : Fancy
# @FileName: config.py
# @Software: PyCharm
# @runstate    ：
# config.py
import os
from dataclasses import dataclass
from dotenv import load_dotenv
from typing import Dict

# 加载 .env 文件
load_dotenv(dotenv_path="config.env")
@dataclass
class DBConfig:
    db_type: str
    connect_args: Dict

    @classmethod
    def from_env(cls, prefix: str) -> 'DBConfig':
        """从环境变量加载配置（推荐用于生产环境）"""
        return cls(
            db_type=os.getenv(f"{prefix}_TYPE"),
            connect_args={
                "host": os.getenv(f"{prefix}_HOST"),
                "user": os.getenv(f"{prefix}_USER"),
                "password": os.getenv(f"{prefix}_PASSWORD"),
                "database": os.getenv(f"{prefix}_DATABASE"),
                "port": os.getenv(f"{prefix}_PORT"),
            }
        )

    #print("mysql_database:", os.getenv("MYSQL_DATABASE"))

    #print ("POSTGRESQL_HOST:", os.getenv ("POSTGRESQL_HOST"))

    # @classmethod
    # def from_file(cls, path: str, profile: str) -> 'DBConfig':
    #     """从配置文件加载（适合本地开发）"""
    #     config = json.loads(Path(path).read_text())
    #     return cls(
    #         db_type=config[profile]["db_type"],
    #         connect_args=config[profile]["connect_args"]
    #     )
